﻿using ImperialWebSite.Presenters.Specie.Classes;
using ImperialWebSite.Presenters.Specie.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ImperialWebSite.Views.Main.Specie
{
    public partial class SpecieRegistry : Page, ISpecieRegistry
    {
        private SpecieRegistryPresenter _presenter;
        private bool _successfulTransaction;

        public string SpecieNameText
        {
            get { return SpecieName.Text; }
        }

        public object SessionCurrentSpecies
        {
            get { return Session["currentSpecies"]; }
            set { Session["currentSpecies"] = value; }
        }


        public string TransactionMessage
        {
            get { return FailureText.Text; }
            set { FailureText.Text = value; }
        }

        public bool SuccessfulTransaction
        {
            get { return _successfulTransaction; }
            set { _successfulTransaction = value; }
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _presenter = new SpecieRegistryPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                InitComponents();
                LoadCurrentSpecies();
                btnRegister.Enabled = SessionCurrentSpecies != null;
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            LoadCurrentSpecies();
            InsertNewSpecie();
        }



        private void InitComponents()
        {
            SuccessfulTransaction = false;
            TransactionMessage = string.Empty;
        }

        private void LoadCurrentSpecies()
        {
            _presenter.LoadCurrentSpecies();
        }

        private void InsertNewSpecie()
        {
            if (SpecieNameText != string.Empty)
            {
                _presenter.InsertNewSpecie();
                if (_successfulTransaction)
                    CleanFields();
            }
        }

        private void CleanFields()
        {
            SpecieName.Text = string.Empty;
        }
    }
}